package com.woniuxy.cloud.carl.carlgoods.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.cloud.carl.carlgoods.client.SpuMannagerClient;
import com.woniuxy.cloud.carl.carlgoods.client.param.AddSpuParam;
import com.woniuxy.cloud.carl.carlgoods.client.param.AddSpuSalesParam;
import com.woniuxy.cloud.carl.carlgoods.common.CarlgoodsResultCode;
import com.woniuxy.cloud.carl.carlgoods.common.ConstData;
import com.woniuxy.cloud.carl.carlgoods.infra.model.Category;
import com.woniuxy.cloud.carl.carlgoods.infra.model.Sku;
import com.woniuxy.cloud.carl.carlgoods.infra.model.Spu;
import com.woniuxy.cloud.carl.carlgoods.service.CategoryService;
import com.woniuxy.cloud.carl.carlgoods.service.SkuService;
import com.woniuxy.cloud.carl.carlgoods.service.SpuService;
import com.woniuxy.exceptions.MallException;
import com.woniuxy.result.Result;
import com.woniuxy.utils.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
@Slf4j
@CrossOrigin("*")
public class SpuMannagerImpl implements SpuMannagerClient {
    @Resource
    private SpuService spuService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private SkuService skuService;
    @Override
    public Result addSpu(AddSpuParam param) {
        Category category = categoryService.getById(param.getCategoryId());
        Assert.state(category!=null,"该类别不存在");
        List<Spu> spus = spuService.list();//名字是否重复
        for (Spu spu : spus) {
           if(spu.getSpuName().equals(param.getSpuName())){
               throw new MallException(CarlgoodsResultCode.DATA_NOT_DOUBLE);
           }
        }
        Spu spu = BeanUtil.toBean(param, Spu.class);
        spu.setCreateDate(new Date());
        spu.setSpuSales(0);
        spu.setSpuStatus(ConstData.SPU_STATE_UP);//上架
        spu.setCategoryName(category.getCategoryName());
        spuService.save(spu);
        return Result.success();
    }

    @Override
    public Result addSpuSales(AddSpuSalesParam param) {
        Map<Long, Integer> map = param.getMap();
        Set<Long> skuIds = map.keySet();
        ArrayList<Spu> spus = new ArrayList<>();
        ArrayList<Sku> skus = new ArrayList<>();
        for (Long skuId : skuIds) {
            Sku sku = skuService.getById(skuId);
            Integer sale = map.get(skuId);
            if(sku!=null){
                sku.setSkuSales(sku.getSkuSales()+sale);
                skus.add(sku);
                Long spuId = sku.getSpuId();
                Spu spu = spuService.getById(spuId);
                if(spu!=null){
                    spu.setSpuSales(spu.getSpuSales()+sale);
                    spuService.updateById(spu);
//                    spus.add(spu);
                }
        }

        }
//        spuService.updateBatchById(spus);
        skuService.updateBatchById(skus);
        return Result.success();
    }
}
